[% setvar title Generalize %]
<div id="archive-notice">
    <h3>This file is part of the Perl 6 Archive</h3>
    <p>To see what is currently happening visit <a href="http://www.perl6.org/">http://www.perl6.org/</a></p>
</div>
<div class='pod'>
<a name='TITLE'></a><h1>TITLE</h1>
<p>Generalize =~ to a special &quot;apply-to&quot; assignment operator</p>
<a name='VERSION'></a><h1>VERSION</h1>
<pre>   Maintainer: Nathan Wiger &lt;<a href='mailto:nate@wiger.org'>nate@wiger.org</a>&gt;
   Date: 29 Aug 2000
   Last-Modified: 16 Sep 2000
   Mailing List: <a href='mailto:perl6-language-regex@perl.org'>perl6-language-regex@perl.org</a>
   Number: 170
   Version: 2
   Status: Frozen</pre>
<a name='ABSTRACT'></a><h1>ABSTRACT</h1>
<p>Currently, <code>=~</code> is only available for use in specific builtin pattern
matches. This is too bad, because it's really a neat operator.</p>
<p>This RFC proposes a simple way to make it more general-purpose.</p>
<a name='NOTES ON FREEZE'></a><h1>NOTES ON FREEZE</h1>
<p>Probably the only way this could be implemented is if <b>RFC 164</b> was
also implemented, freeing <code>=~</code> for use as a more general-purpose
operator. Indeed, a main point of this RFC is to provide a means for a
backwards-compatible syntax for regex's.</p>
<p>Unlink <b>RFC 164</b>, most people I heard from liked this. Some criticized
it as being too sugary, since this:</p>
<pre>   $string =~ quotemeta;    # $string = quotemeta $string;</pre>
<p>Is not as clear as the original. However, there is fairly similar
precedent in:</p>
<pre>   $x += 5;                 # $x = $x + 5;</pre>
<p>And to me it seems to be quite clear that <code>quotemeta</code> is acting on
<code>$string</code> in the above example, even when you take into account <code>=~</code>'s
current binding meaning (perhaps more so, in fact).</p>
<a name='DESCRIPTION'></a><h1>DESCRIPTION</h1>
<p>First off, this assumes RFC 164. Second, it requires you drop any
knowledge of how <code>=~</code> currently works. Finally, it runs directly
counter to RFC 139, which proposes another application for <code>=~</code>.</p>
<p>This RFC proposes a simple use for <code>=~</code> as a generic &quot;apply-to&quot;
operator. When used, any values on the left side of the expression are
implicitly passed to the end of the right-side expression. What this
means is that an expression such as this:</p>
<pre>   $value = dostuff($arg1, $arg2, $value);</pre>
<p>Could now be rewritten as:</p>
<pre>   $value =~ dostuff($arg1, $arg2);</pre>
<p>And <code>$value</code> would be implicitly transferred over to the right side as
the last argument. It's simple, but it makes what is being operated on
quite obvious.</p>
<p>This enables us to rewrite the following constructs:</p>
<pre>   $string = quotemeta($string);
   @array = reverse @array;
   ($name) = split /\s+/, $name;
   @vals = sort { $a &lt;=&gt; $b } @vals;
   @file = grep !/^#/, @file;

   $string = s/\s+/SPACE/, $string;    # RFC 164
   $matches = m/\w+/, $string;         # RFC 164
   @strs = s/foo/bar/gi, @strs;        # RFC 164</pre>
<p>As the shorter and more readable:</p>
<pre>   $string =~ quotemeta;
   @array =~ reverse;
   ($name) =~ split /\s+/;
   @vals =~ sort { $a &lt;=&gt; $b };
   @file =~ grep /!^#/;

   $string =~ s/\s+/SPACE/;            # looks familiar
   $string =~ m/\w+/;                  # this too [1]
   @strs =~ s/foo/bar/gi;              # cool extension</pre>
<p>It's a simple solution, true, but it has a good amount of flexibility
and brevity. It could also be the case that multiple values could be
called and returned, so that:</p>
<pre>   ($name, $email) = special_parsing($name, $email);</pre>
<p>Becomes:</p>
<pre>   ($name, $email) =~ special_parsing;</pre>
<p>Again, it's simple, but seems to have useful applications. One nice
thing is that in many (most?) situations it appears to be working very
much like <code>=~</code> currently works with regex's (from a user perspective).</p>
<p>Finally, note this can only work with functions and function-like
constructs. An attempt to do something like this:</p>
<pre>   $x =~ 5 +;</pre>
<p>Should <i>definitely</i> remain a syntax error.</p>
<a name='Possible addition of ~= operator'></a><h2>Possible addition of <code>~=</code> operator</h2>
<p>A symmetric operator, <code>~=</code>, was proposed informally on the list which
would left-pad the argument list:</p>
<pre>    $stuff =~ dojunk(@args);   # $stuff = dojunk(@args, $stuff);
    $stuff ~= dojunk(@args);   # $stuff = dojunk($stuff, @args);</pre>
<p>but the consensus that I received was about 50/50: half liked it, half
thought it was too confusing. Even though we don't have a <code>bitnot=</code>
operator currently, creating something that looks like one but acts
completely differently is probably not a good idea.</p>
<p>If something like this was included, it would probably be best to go
with another operator, like <code>=^</code>:</p>
<pre>    $stuff =~ dojunk(@args);   # $stuff = dojunk(@args, $stuff);
    $stuff =^ dojunk(@args);   # $stuff = dojunk($stuff, @args);</pre>
<p>But that's awfully close to <code>^=</code>. Hmmm. Regardless, this operator is
unlikely to be used nearly as widely since Perl functions usually take
the argument to act on in the last position.</p>
<a name='IMPLEMENTATION'></a><h1>IMPLEMENTATION</h1>
<p>Simplistic (hopefully). Should just involve stacking values onto a
function's argument list.</p>
<a name='MIGRATION'></a><h1>MIGRATION</h1>
<p>This introduces new functionality, which allows backwards compatibility
for regular expressions. As such, it should require no special
translation of code. This RFC assumes RFC 164 will be adopted (which it
may not be) for changes to regular expressions.</p>
<a name='NOTES'></a><h1>NOTES</h1>
<p>[1] That m// one doesn't quite work right, but that's a special case
that I would suggest should be caught by some other part of the grammar
to maintain backwards compatability (like bare //).</p>
<a name='REFERENCES'></a><h1>REFERENCES</h1>
<p>RFC 164: Replace =~, !~, m//, s///, and tr/// with match(), subst(), and
trade()</p>
<p>RFC 139: Allow Calling Any Function With A Syntax Like s///</p>
<p>Bart Lateur's suggestion of using the term &quot;apply-to&quot;:
<a href='http://www.mail-archive.com/perl6-language-regex@perl.org/msg00182.html' target='_blank'>www.mail-archive.com</a></p>
</div>
